/*
Copyright 2009 Peter Krajnc, SCREENWORKER

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

  http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package com.screenworker.compactmvc.examples.flex.model
{
	import com.screenworker.compactmvc.examples.flex.model.vo.DocumentVO;
	import com.screenworker.compactmvc.examples.flex.view.DocumentView;
	import com.screenworker.compactmvc.interfaces.IModel;
	import com.screenworker.compactmvc.patterns.Controller;
	import com.screenworker.compactmvc.patterns.Model;

	public class ApplicationViewModel extends Model implements IModel
	{
		public static const NAME:String = 'ApplicationViewModel';
		
		[Bindable]
		public var applicationTitle:String;
		
		[Bindable]
		public var removeButtonEnabled:Boolean = false;

		[Bindable]
		public var documents:Array = [];
		
		[Bindable]
		public var documentsDataProvider:Array;
		
		public var currentDocument:DocumentVO;
		
		[Bindable]
		public var moduleLoaded:Boolean = false;
		
		[Bindable]
		public var messageList:String = '';
		
		[Bindable]
		public var newButtonEnabled:Boolean = true;
		
		[Bindable]
		public var selectedDocument:int = 0;
		
		public function ApplicationViewModel(name:String, controller:Controller)
		{
			super(name, controller);
		}
		
		public function addDescription(docId:int, description:String):Boolean
		{
			for (var i:int = 0; i < documents.length; i++)
			{
				if(DocumentVO(documents[i]).id == docId)
				{
					DocumentVO(documents[i]).remoteDescription = description;
					return true;
				}
			}
			return false;
		}
		
		public function addDocument():void
		{
			var doc:DocumentVO = new DocumentVO();
			doc.id = randomRange(100, 1000000);
			doc.name = 'Document ' + doc.id;
			doc.type = 'default';
			doc.classRef = DocumentView;
			documents.push(doc);
			
			refreshDocumentsDataProvider();
			
			currentDocument = doc;
			removeButtonEnabled = true;
			newButtonEnabled = documents.length < 5;
		}
		
		public function randomRange (min:Number, max:Number):int 
		{
			return Math.floor (Math.random () * (max - min + 1)) + min;	
		}
	
		public function removeDocument(doc:DocumentVO):Boolean
		{
			for (var i:int = 0; i < documents.length; i++)
			{
				if(documents[i] == doc)
				{
					documents.splice(i, 1);
					refreshDocumentsDataProvider();
					return true;
				}
			}
			
			return false;
		}
		
		public function removeCurrentDocument():Boolean
		{
			for (var i:int = 0; i < documents.length; i++)
			{
				if(documents[i] == currentDocument)
				{
					documents.splice(i, 1);
					refreshDocumentsDataProvider();
					
					removeButtonEnabled = documents.length > 0;	
					newButtonEnabled = documents.length < 5;	
			
					return true;
				}
			}			
			return false;
		}
		
		
		private function refreshDocumentsDataProvider():void
		{
			documentsDataProvider = [];
			for each(var item:DocumentVO in documents)
			{
				documentsDataProvider.push({label:item.name, data:item});
			}
		}
		
		
	}
}
